from flask import jsonify, request, send_file, current_app
from flask_jwt_extended import (
    create_access_token,
    create_refresh_token,
    jwt_required,
    get_jwt_identity
)
from .models import *
from .auth.login import *  # 更新导入路径
import os
from .auth.upload import *
from .auth.recommend import*


def register_routes(app, limiter, jwt):#限制路由函数
    @app.route('/', methods=['GET'])
    def hello():
        # 使用相对路径
        return send_file(os.path.join(current_app.root_path, 'static', 'test.html'))
    
    @app.route('/register', methods=['POST'])
    @limiter.limit("3 per minute")
    def register1():
        return register()
    
    @app.route('/getTicket', methods=['GET'])
    def getTicket1():
        deviceId = request.args.get('deviceId')
        return getTicket(deviceId)
    
    @app.route('/getToken', methods=['POST'])
    def getDeviceToken():
        return getToken()

    @app.route('/refreshToken', methods=['POST'])
    def refreshtoken1():
        return refreshtoken()

    @app.route('/uploadData', methods=['POST'])
    def uploaData1():
         return uploadData()
     
    # @app.route('/recommend/sleeptime', methods=['GET'])  
    # def sleep_recommendation1():
    #     return sleep_recommendation()
    
    # @app.route('/wearingtime', methods=['GET'])
    # def wearingtime1():
    #     return wearingtime()
    
    # @app.route('/recommend/walkpace', methods=['GET'])
    # def walkpace1():
    #     return walkpace()
    
    # @app.route('/recommend/breath', methods=['GET'])  
    # def breath1():
    #     return breath()
    
    # @app.route('/get', methods=['GET'])  
    # def get1():
    #     return getdata()
         
         
    @app.errorhandler(404)
    def not_found_error(error):
        return jsonify({'error': 'Resource not found'}), 404

    @app.errorhandler(429)
    def ratelimit_handler(e):
        return {
            "msg": "You have exceeded the rate limit of 3 requests per minute."
        }, 201
   